package com.shf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shf.entity.Permission;
import com.shf.entity.Role;
import com.shf.mapper.RoleMapper;
import com.shf.mapper.PermissionMapper;
import com.shf.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**角色管理业务实现类
 * @author 叶枫
 */
@Service("rs")
public class RoleServiceImpl implements RoleService {
    /**持久层属性*/
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public PageInfo<Role> findAllRole(int page, int size) {
        PageHelper.startPage(page,size);
        List<Role> roleList = roleMapper.findAllRole();
        return new PageInfo<Role>(roleList);
    }

    @Override
    public void saveRole(Role role) {
        roleMapper.saveRole(role);
    }

    @Override
    public Role findRoleById(String id) {
        return roleMapper.findRoleById(id);
    }

    @Override
    public List<Permission> findAllPermission() {
        return permissionMapper.findAllPermission();
    }

    @Override
    public void addPermissionToRole(String roleId,String [] ids) {

        for (String id : ids) {
            roleMapper.addPermissionToRole(roleId,ids);
        }

    }

    @Override
    public Role roleDetail(String id) {
        return roleMapper.roleDetailById(id);
    }

    @Override
    public void deleteRole(String id) {
        roleMapper.deleteRole(id);

        permissionMapper.deleteRolePermission(id);

    }

    @Override
    public void deleteMany(String[] ids) {
        roleMapper.deleteManyRole(ids);
        permissionMapper.deleteManyRolePermission(ids);
    }
}
